package cn.cxyxj.code_auth_jwt_persistence_prod.common;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * Api接口响应体
 *
 * @author: cxyxj
 * @create: 2022-01-17 13:56
 */
@Data
@Accessors(chain = true)
public class ApiResult<T> {

    /**
     * 结果 coed，0 正确，其余都是异常
     */
    private Integer code;

    /**
     * 错误信息
     */
    private String msg;

    /**
     * 业务数据
     */
    private T data;

    private ApiResult() {
    }

    /**
     * 成功
     */
    public static ApiResult success() {
        ApiResult r = new ApiResult();
        r.setCode(ApiResultEnum.SUCCESS.getCode());
        r.setMsg(ApiResultEnum.SUCCESS.getMsg());
        return r;
    }

    public static <T> ApiResult success(T data) {
        ApiResult r = new ApiResult();
        r.setCode(ApiResultEnum.SUCCESS.getCode());
        r.setMsg(ApiResultEnum.SUCCESS.getMsg());
        r.setData(data);
        return r;
    }

    /**
     * 失败
     */
    public static ApiResult fail() {
        ApiResult r = new ApiResult();
        r.setCode(ApiResultEnum.FAIL.getCode());
        r.setMsg(ApiResultEnum.FAIL.getMsg());
        return r;
    }

    public static <T> ApiResult fail(T data) {
        ApiResult r = new ApiResult();
        r.setCode(ApiResultEnum.FAIL.getCode());
        r.setMsg(ApiResultEnum.FAIL.getMsg());
        r.setData(data);
        return r;
    }


    public static ApiResult fail(Integer code) {
        ApiResult r = new ApiResult();
        r.setCode(code);
        r.setMsg(ApiResultEnum.FAIL.getMsg());
        return r;
    }

    public static ApiResult fail(String msg) {
        ApiResult r = new ApiResult();
        r.setCode(ApiResultEnum.FAIL.getCode());
        r.setMsg(msg);
        return r;
    }

    public static <T> ApiResult fail(ApiResultEnum resultCode) {
        ApiResult r = new ApiResult();
        r.setCode(resultCode.getCode());
        r.setMsg(resultCode.getMsg());
        return r;
    }

    public static ApiResult fail(Integer code, String msg) {
        ApiResult r = new ApiResult();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }


    public static <T> ApiResult fail(ApiResultEnum resultCode, T data) {
        ApiResult r = new ApiResult();
        r.setCode(resultCode.getCode());
        r.setMsg(resultCode.getMsg());
        r.setData(data);
        return r;
    }

}
